<template>
     <bi-form >
        <bi-form-item label="数据库">
            <bi-select v-model="config" required style="margin-bottom:10px;" :clear="false">
                <bi-select-option  v-for="item in configList" :key="item.value" :value="item.value" :label="item.label" ></bi-select-option>
            </bi-select>
        </bi-form-item>
        <bi-table :data="tableList" :loading="loading" height="300" checkbox  class="border raduis" @checkboxChange="checkboxChange" >
            <bi-table-column label="模型" name="name" width="200" ></bi-table-column>
            <bi-table-column label="表名" name="table" width="200" ></bi-table-column>
            <bi-table-column label="备注" name="comment"  ></bi-table-column>
        </bi-table>
    </bi-form>
</template>

<script>
export default{
    dialog:{
        width:600,
        title:'安装数据库',
        loading:false,
        okButton:'立即安装'
    },
    data(){
        return {
            config:'default',
            configList:[],
            tableList:[],
            modelList:[],
            checkList:[],
            loading:false
        }
    },
    props:{
        app:String
    },
    watch:{
        config(val){
            this.flashTable(val);
        }
    },
    methods:{
        checkboxChange(list){
            this.checkList = list;
        },
        dialogOk(){

            if (this.checkList.length==0) {
                this.$alert('请选择需要安装的表');
                return false;
            }

            let list = [];

            this.checkList.map(item=>{
                list.push(item.name);
            });

            this.$confirm({
                type:'warning',
                title:'确认安装',
                text:'请确保在安装数据库没有数据，保证安装文件没有被修改，否则可能会造成数据丢失！'
            },()=>{
                this.dialog.loading = true;
                this.$ajax({
                    url:this.$url({action:'setupDatabase'}),
                    data:{
                        app:this.app,
                        config:this.config,
                        list:list
                    },
                    success:(res)=>{
                        this.$alert('安装成功');
                    },
                    over:()=>{
                        this.dialog.loading = false;
                    }
                })
            });

            


            return false;
        },
        flashTable(val){

            let driver = '';
            
            this.loading = true;
            this.configList.map(config=>{
                if (config.value==val) driver = config.driver;
            });

            this.tableList = this.modelList.filter(model=>{
                return model.sql.indexOf(driver)!==-1; 
            });
            
            this.$nextTick(()=>{
                console.log(this.tableList);
                this.loading = false;
            });
        }
    },
    mounted(){
        this.$ajax({
            url:this.$url({action:'databaseConfigList'}),
            data:{
                app:this.app
            },
            success:(res)=>{
                
                this.configList = res.config ;
                this.config = '';
                
                res.config.map(config=>{
                    if (config.value=='default' || this.config =='') this.config = config.value;
                })
               
                this.modelList = res.model;
                
                this.$nextTick(()=>{
                    this.flashTable(this.config);
                });
                this.dialog.loading = false;
            }
        })
    }
}
</script>